package cn.com.thingo.hyintelligent.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.com.thingo.hyintelligent.R;
import cn.com.thingo.hyintelligent.model.bean.GatewayDetailBean;
import cn.com.thingo.hyintelligent.model.http.api.ShouCheApis;

public class AuthListAdapter extends RecyclerView.Adapter<AuthListAdapter.ViewHolder> {

    private List<GatewayDetailBean.DataBean.UserBean> list;
    private Context context;
    private OnAuthDeleteListener listener;

    public AuthListAdapter(List<GatewayDetailBean.DataBean.UserBean> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ViewHolder(LayoutInflater.from(context).inflate(R.layout.item_auth_list, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        GatewayDetailBean.DataBean.UserBean userBean = list.get(position);
        Glide.with(context).load(ShouCheApis.HOST_ + userBean.getImage()).apply(new RequestOptions().error(R.mipmap.default_head)
                .placeholder(R.mipmap.default_head).circleCrop().diskCacheStrategy(DiskCacheStrategy.ALL)).into(holder.ivHead);
        holder.tvName.setText(userBean.getTelephone());
        if (listener != null) {
            holder.ivDelete.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.authDelete(position);
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_auth_head)
        ImageView ivHead;
        @BindView(R.id.item_auth_name)
        TextView tvName;
        @BindView(R.id.item_auth_delete)
        ImageView ivDelete;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public void setListener(OnAuthDeleteListener listener) {
        this.listener = listener;
    }

    public interface OnAuthDeleteListener {
        void authDelete(int position);
    }
}
